<?php $this->JQuery()->addJavascriptFile('http://bp.yahooapis.com/2.4.21/browserplus-min.js')?>

<?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.v-'.AC_VERSION.'.js'))?>
<?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.html5.v-'.AC_VERSION.'.js'))?>
<?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.html4.v-'.AC_VERSION.'.js'))?>

<?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/jquery.plupload.queue.min.v-'.AC_VERSION.'.js'))?>
<?php if (IS_LIVE):?>
    <?php $this->headLink()->appendStylesheet(Ac_Util::getImageBaseUrl('/css/plupload/plupload.queue.v-'.AC_VERSION.'.css'))?>
<?php endif;?>

<?php 
    if (!empty($this->listing)) {
        $type = 'listing';
    } elseif(!empty($this->user)) {
        $type = 'user';
    } else {
        $type = '';
    }
?>

<?php $this->JQuery()->javascriptCaptureStart()?>
var uploader;
var dialog;
$(function() {

    $('#change_uploader').click(function() {
        if ($('#form_uploader').css('display') == 'none') {
            $('#change_uploader').html('请尝试使用基本上传模式。');
            $('#form_uploader_default').hide();
            $('#form_uploader').removeClass('hide');
            $('#form_uploader').show();
        } else {
            
            $('#change_uploader').html('<?php echo $this->t('Use the fast uploader')?>');
            $('#form_uploader_default').show();
            $('#form_uploader').hide();    
            $('#form_uploader_default').removeClass('hide');
        }
        return false;
    });
    
    if ($.browser.msie) {
        $('#form_uploader_default').show();
        $('#form_uploader').hide();
        $('#change_uploader').html('<?php echo $this->t('Use the fast uploader')?>');        
        $j('.plupload').css('top', '15px');
	    $j('.plupload').css('left', '295px');
    } else {
        $('#form_uploader_default').hide();
        $('#form_uploader_default').removeClass('hide');
        $('#form_uploader').show();
        $('#change_uploader').html('请尝试使用基本上传模式。');
        $j('.plupload').css('top', '15px');
	    $j('.plupload').css('left', '295px');
    }

	uploader = new plupload.Uploader({
		runtimes : 'html5,flash,html4',
		browse_button : 'pickfiles',
		container : 'container_uploader',
		max_file_size : '10mb',
        <?php if (!empty($this->listing)):?>
    		url : '/default/upload/listing-photo',
        <?php elseif (!empty($this->company)):?>
    		url : '/default/upload/company-photo',
        <?php endif;?>
		flash_swf_url : '/plupload/js/plupload.flash.swf',
		silverlight_xap_url : '/plupload/js/plupload.silverlight.xap',
        headers: {adid: $('#adid').attr('value'), ad_type: "<?php echo $type?>"},
		filters : [
			{title : "Image files", extensions : "jpeg,jpg,gif,png"}
		]
		
	});

	uploader.bind('Init', function(up, params) {
		//$('#filelist').html("<div>Current runtime: " + params.runtime + "</div>");
        console.log("Uploader init: Current runtime: " + params.runtime );
	});

	$('#uploadfiles').click(function(e) {
		uploader.start();
		e.preventDefault();
	});

	//uploader.init();
        
        uploader.bind('FilesAdded', function(up, files) {
            $.each(files, function(i, file) {
                $('#container_uploader').before(
                        '<div id="' + file.id + '">' +
                        file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
                '</div>');
            });
            up.refresh(); // Reposition Flash/Silverlight        
        });
        
    /*
	uploader.bind('FilesAdded', function(up, files) {
		$.each(files, function(i, file) {
			$('#container_uploader').before(
				'<div id="' + file.id + '">' +
				file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
			'</div>');
		});
		up.refresh(); // Reposition Flash/Silverlight
	});
    */

	uploader.bind('UploadProgress', function(up, file) {
            $('#' + file.id + " b").html(file.percent + "%");
            if (file.percent == 100) {
                $('#' + file.id).remove();
            }
	});

	uploader.bind('Error', function(up, err) {
                if (err.code == -600) {
                    $('#container_uploader').before('<ul id="plupload_error" class="errors"><li><?php echo $this->t('The file you are trying to upload is too large')?></li></ul>');
                    $('#plupload_error').delay(2000).fadeOut();
                    
                } else {
                    $('#container_uploader').before("<div>Error: " + err.code +
			", Message: " + err.message +
			(err.file ? ", File: " + err.file.name : "") +
			"</div>"
                    );
                }
		
		up.refresh(); // Reposition Flash/Silverlight
		
        $j('.plupload').css('top', '15px');
	    $j('.plupload').css('left', '295px');
	});

	uploader.bind('FileUploaded', function(up, file, res) {
        var response = $.parseJSON(res.response);
		$('#' + file.id + " b").html("100%");
        var nb_photo = $('#profile_pic_cnt').attr("rel");

        if (nb_photo == 0) {
            var div = $(document.createElement("div")).attr("id", "photo"+nb_photo).attr("class", "selected");
            $j('#no_photo_selected').hide();
        } else {
            var div = $(document.createElement("div")).attr("id", "photo"+nb_photo);
        }

        $(document.createElement("img"))
            .attr({ src: response.url})
            .appendTo($(document.createElement("figure")).appendTo(div));
        $j('.plupload').css('top', '15px');
	    $j('.plupload').css('left', '295px');
        $(document.createElement("a"))
            .attr({ "class": "remove_photo", rel:nb_photo})
            .html("<?php echo $this->t('Remove')?>")
            .click(function() {
                    var nb = $(this).attr('rel');
                    $j.post(
                        <?php if (!empty($this->listing)):?>
                            '<?php echo $this->geturl('delete-photo','listing')?>',
                        <?php elseif (!empty($this->company)):?>
                            '<?php echo $this->geturl('delete-photo','user')?>',
                        <?php endif;?>
                        {adid:$j('#adid').attr('value'), num:nb},
                        function (data) {
                            $('#photo'+nb).remove();
                            $('#profile_pic_cnt').attr("rel", parseInt($('#profile_pic_cnt').attr("rel")) - 1);
                            var i = 0;
                            $('.remove_photo').each(function() {
                                $(this).attr('rel', i++);
                            });                        
                            var i = 0;
                            $('.feature_photo').each(function() {
                                $(this).attr('rel', i++);
                            });  
                        }
                    );
                    return false;
                })
            .appendTo(div);

        var txt = '';
        if (nb_photo == 0) {
            txt = "封面图片";
        } else {
            txt = "设置为封面图片";
        }

        $(document.createElement("a"))
            .attr({ "class": "feature_photo", rel:nb_photo})
            .html(txt)
            .click(function() {featurePhoto($(this), nb_photo);return false;})
            .appendTo(div);

        div.appendTo($('#profile_pic_cnt'));
        $('#profile_pic_cnt').attr("rel", parseInt($('#profile_pic_cnt').attr("rel")) + 1);
	});
    
    uploader.init();

    uploader.bind('FilesAdded', function(up, files) {
        uploader.start();
    });

    $j('.remove_photo').click(function() {
        var nb = $(this).attr('rel');
       
            $j.post(
                <?php if (!empty($this->listing)):?>
                   '<?php echo $this->geturl('delete-photo','listing')?>',
                <?php elseif (!empty($this->company)):?>
                   '<?php echo $this->geturl('delete-photo','user')?>',
                <?php endif;?>
                {adid:$j('#adid').attr('value'), num:nb},
                function (data) {
                        $('#photo'+nb).remove();
                        $('#profile_pic_cnt').attr("rel", parseInt($('#profile_pic_cnt').attr("rel")) - 1);
                        var i = 0;
                        $('.remove_photo').each(function() {
                            $(this).attr('rel', i++);
                        });                        
                        var i = 0;
                        $('.feature_photo').each(function() {
                            $(this).attr('rel', i++);
                        });     
							                                           
                }
            );
       
        return false;
    });

    function featurePhoto(ele, nb) {
        //var nb = ele.attr('rel');
        var obj = ele;

        $j.post(
            '<?php echo $this->geturl('feature-photo','listing')?>',
            {adid:$j('#adid').attr('value'), id:nb},
            function (data) {
                var i=0;
                $('.feature_photo').html("封面图片");
                obj.html("设置为封面图片");
            }
        );

        $j('#profile_pic_cnt>div').removeClass('selected');
        $j('#photo' + nb).addClass('selected');

        return false;
    }

    $j('.feature_photo').click(function() {featurePhoto($(this), $(this).attr('rel')); return false});
    $j('#container_uploader').css('position', 'static');
    $j('.plupload').css('left', '295px');
    $j('.plupload').css('top', '15px');
});
<?php $this->JQuery()->javascriptCaptureEnd()?>
<div id="form_uploader_cnt" class="photo_uploader">
    <div id="form_uploader" class="button">
        <label>上传图片</label>
        <span class="remark">请上传格式为JPEG，JPG，PNG或TIF的图片，每张不超过4M。</span>
        <div id="container_uploader" class="button">
            <button id="pickfiles" type="button">浏览...</button>
        </div>
    </div>

    <div id="form_uploader_default" class="hide">
        <label>上传图片</label>
        <span class="remark">请上传格式为JPEG，JPG，PNG或TIF的图片，每张不超过4M。</span>
        <div class="file">
            <label>封面图片</label>
            <input type="file" id="file_upload_1" name="upload1">
        </div>
        <div class="file">
            <label>图片2</label>
            <input type="file" id="file_upload_2" name="upload2">    
        </div>
        <div class="file">
            <label>图片3</label>
            <input type="file" id="file_upload_3" name="upload3">    
        </div>    
        <div class="file">
            <label>图片4</label>
            <input type="file" id="file_upload_4" name="upload4">    
        </div>    
    </div>
     
    <?php
        $photos = array();
        $featured_photo_id = 0;
        if (!empty($this->listing)) {
            $photos = $this->listing->photos;
            $featured_photo_id = (int)$this->listing->featured_photo_id;
        } elseif(!empty($this->company)) {
            $photos = (array)$this->company->photos;
            $featured_photo_id = (int)$this->company->featured_photo_id;
        }
        //ar_dump($photos, $featured_photo_id);
    ?>

    <div rel="<?php echo count($photos)?>" id="profile_pic_cnt">
        <?php if(empty($photos)):?>
    	    <p id="no_photo_selected">没有选定图片</p>
        <?php endif;?>
        <?php foreach($photos as $k => $photo):?>
            <div id="photo<?php echo $k?>" <?php if ($featured_photo_id == $k) { echo "class='selected'"; } ?>>
                <figure>
                    <img src="<?php echo $this->thumbnail()->scale($photo, 150, 150)?>">
                </figure>
                <a href="#" class="remove_photo" rel="<?php echo $k?>">移除</a>
                <?php if ($featured_photo_id != $k):?>
                    <a href="#" class="feature_photo" rel="<?php echo $k?>">封面图片</a>
                <?php else:?>
                    <a href="#" class="feature_photo" rel="<?php echo $k?>">设置为封面图片</a>
                <?php endif;?>
            </div>
        <?php endforeach;?>
    </div>
    <p>无法上传图片？<a id="change_uploader" href="#">请尝试使用基本上传模式。</a>.</p>
</div>
